/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.aptech.bluepumpkin.Contact;
import com.aptech.bluepumpkin.Users;
import com.aptech.bluepumpkin.beans.ContactFacade;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;
import utils.Uility;

/**
 *
 * @author Chien
 */
@ManagedBean
@SessionScoped
public class ContactControl {

    @EJB
    private ContactFacade contactFacade;
    private Contact contact;
    private Contact selectedContact;
    private String replyContent;
   private Users user = new Users();

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

   
    public List<Contact> getAllContact() {
        return contactFacade.findAll();
    }

    public void insert() {
        try {
            contactFacade.create(contact);
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/ContactManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void replyContact(Contact c) {
        selectedContact = c;
        user = selectedContact.getCreatedBy();
        if (null != user && null != user) {
            System.out.println("Reply to---"+user.getFullName());
        }else{
            System.out.println("-----User is null-----on Selected Contact-----");
        }
        redirectToReplyContact();
    }

    public void sendMail() {
        try {
            
            if (null == user.getEmail() || user.getEmail().trim().isEmpty()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sorry, User has no email to send mail to.", ""));
                return;
            }
            String subject ="RE: "+selectedContact.getTitle();
            Uility.sendMail(user.getEmail(),subject, getReplyContent());
            selectedContact.setIsAnswered(true);
            contactFacade.edit(selectedContact);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Reply has been sent.", ""));
        } catch (MessagingException ex) {
            Logger.getLogger(ContactControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(Contact c) {
        selectedContact = c;
        contactFacade.remove(selectedContact);
    }

    public void redirectToListContact() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/contact/contactPage.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void redirectToReplyContact() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/contact/replyContactPage.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Contact getSelectedContact() {
        return selectedContact;
    }

    public void setSelectedContact(Contact selectedContact) {
        this.selectedContact = selectedContact;
    }

    public String getReplyContent() {
        return replyContent;
    }

    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
    }
}
